$(function () {

    var up = $("#up");
    var down = $("#down");
    var first = $("#first");
    var second = $("#second");
    var third = $("#third");
    var last = $("#last");
    var currentNo = $("#pageNo");
    var pageNo = 1;

    var p1 = first.attr("name");
    var p2 = second.attr("name");
    var p3 = third.attr("name");
    var form = $("#form");

    up.bind("click",function () {
        pageNo = Number(currentNo.val())-1;
        if(pageNo>1){
            currentNo.val(pageNo);
            sub(pageNo)
        }else {
            currentNo.val(1);
            sub(1);
        }
    });

    down.bind("click",function () {
        pageNo = Number(currentNo.val())+1;
        if(pageNo<last.attr("name")){
            currentNo.val(pageNo);
            sub(pageNo);
        } else{
            currentNo.val(last.attr("name"));
            sub(last.attr("name"));
        }
    });

    last.bind("click",function () {
        var vlast = last.attr("name");
        sub(vlast);
    });

    first.bind("click",function () {
        currentNo.val(1);
        sub(p1);
    });

    second.bind("click",function () {
        currentNo.val(p2);
        sub(p2);
    });

    third.bind("click",function () {
        currentNo.val(p3);
        sub(p3);
    });


    $.ajax({
        url : "easybuy/news/getList",
        type : "GET",
        data :  {"pageIndex":pageNo},
        dataType : "json",
        success : function (data) {
            last.attr("name",data.pageCount);
            var th = "";
            th +="<tr style='font-size: 25px;height: 40px'><td style='width: 50%'><strong>文章标题</strong></td>";
            th +="<td style='50%'><strong>创建时间</strong></td></tr>";

            form.append(th);
            for(var i=0;i<data.list.length;i++){
                form.append("<tr style='font-size: 20px;height: 40px'>" +
                    "<td><a href='News_Content.html?id="+data.list[i].id+"'>"+data.list[i].title+"</a></td>"+
                    "<td>"+data.list[i].createTime+"</td></tr>"
                );
            }
        },
        error : function () {
            alert("出现错误");
        }
    });

    function sub(pageNo){
        form.html("");
        //alert($("#pageNo").val());
        $.ajax({
            url : "easybuy/news/getList",
            type : "GET",
            data :  {"pageIndex":pageNo},
            dataType : "json",
            success : function (data) {
                last.attr("name",data.pageCount);
                var th = "";
                th +="<tr style='font-size: 25px;height: 40px'><td style='width: 50%'><strong>文章标题</strong></td>";
                th +="<td style='50%'><strong>创建时间</strong></td></tr>";

                form.append(th);
                for(var i=0;i<data.list.length;i++){
                    form.append("<tr style='font-size: 20px;height: 40px'>" +
                        "<td><a href='News_Content.html?id="+data.list[i].id+"'>"+data.list[i].title+"</a></td>"+
                        "<td>"+data.list[i].createTime+"</td></tr>"
                    );
                }
            },
            error : function () {
                alert("出现错误");
            }
        });
    }

});